<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Models\ClientCar as Car;
use App\Models\Cartype;
use App\Models\CarBrand;
use App\Models\CarSerie;
use App\Models\CarModel;
use App\Repos\CarRepo;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\CarExport;

class CarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        $query = Car::query();
        if($request->client_id) {
            $query->where(['client_id'=>$request->client_id]);
        }
        if($request->keyword) {
            $query->where('car_num','like','%'.$request->keyword.'%');
            $where['keyword'] = $request->keyword;
        }
        $list = $query->paginate(20);
        return view('admin.car.index',compact('list','where'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $clients = Client::get();
        $brands  = CarBrand::get();
        $types   = Cartype::get();
        return view('admin.car.add',compact('brands','types','clients'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except(['_token']);        
        $flag = Car::create($data);
        if($flag) {
            return redirect('zadmin/cars');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data    = Car::find($id);
        $clients = Client::get();
        $brands  = CarBrand::get();
        $types   = Cartype::get();
        $series  = CarSerie::get();
        $models  = CarModel::get();
        return view('admin.car.edit',compact('data','brands','types','clients','series','models'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        $data['updater'] = session('admin')->username;
        $rs   = Car::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/cars');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'danger';        
        $flag = CarRepo::destroy($id);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return back()->with('rs',$rs);
        }
        $rs['status'] = 'success';
        $rs['msg'] = '操作成功';
        return back()->with('rs',$rs);
    }

    //导出工单。
    public function carExport(Request $request)
    {
        $param = [];
        if($request->client_id) {
            $param['keyword'] = (int)$request->keyword;
        }
        return Excel::download(new CarExport($param), '车辆列表.xlsx');   
    }

    public function batchDel(Request $request)
    {
        $ids = $request->ids;
        Car::whereIn('id',$ids)->delete();
        $rs['status'] = true;
        $rs['msg'] = '操作成功';
        return response()->json($rs);
    }
}
